/**
 * Created by Administrator on 2016/5/22.
 */

var $bannerCount = $(".banner-list-item");

var length = $bannerCount.length;
var timer = null;

/*automatic*/
function scroll() {
    var $trg = $(".banner-list-item.active");
    var curIndex = $bannerCount.index($trg);
    $trg.removeClass("active");

    if(curIndex == length-1) {
        $(".banner-list-item:eq(0)").addClass("active");
        curIndex = 0;
    } else {
        $trg.next().addClass("active");
        curIndex++;
    }
    $(".banner-control li").eq(curIndex).addClass("active").siblings().removeClass("active");
}

timer = setInterval(scroll,5000);

$(".banner-next").click(function () {
    var curIndex = $bannerCount.index($(".banner-list-item.active"));
    if(curIndex == $bannerCount.length-1) {
        $bannerCount.eq(0).addClass("active").siblings().removeClass("active");
        $(".banner-control li").eq(0).addClass("active").siblings().removeClass("active");
    } else {
        $bannerCount.eq(curIndex+1).addClass("active").siblings().removeClass("active");
        $(".banner-control li").eq(curIndex+1).addClass("active").siblings().removeClass("active");
    }
});

$(".banner-pre").click(function () {
    var curIndex = $bannerCount.index($(".banner-list-item.active"));
    if(curIndex == 0) {
        $bannerCount.eq($bannerCount.length-1).addClass("active").siblings().removeClass("active");
        $(".banner-control li").eq($bannerCount.length-1).addClass("active").siblings().removeClass("active");
    } else {
        $bannerCount.eq(curIndex-1).addClass("active").siblings().removeClass("active");
        $(".banner-control li").eq(curIndex-1).addClass("active").siblings().removeClass("active");
    }
});

$(".banner").mouseenter(function () {
    clearInterval(timer);
});

$(".banner").mouseleave(function () {
    timer = setInterval(scroll,3000);
});

$(".banner-control li").click(function() {
    var pos = $(".banner-control li").index($(this));
    if(!$(this).hasClass("active")) {
        $(this).addClass("active").siblings().removeClass("active");
        $($bannerCount[pos]).addClass("active").siblings().removeClass("active");

    }
});